/**
 * Created by lenovo on 2018/5/23.
 */

function createInit(opts) {
  var option = {
    /* moduleType:默认false , false表示编辑，true表示显示 */
    moduleType: opts.moduleType || false,
    hasControls: opts.hasControls || false,
    /* true禁止拖拽大小的缩放，false拖拽大小缩放 */
    selectable: opts.selectable || false,
    /* false禁止拖拽,true拖拽 */
    editable: opts.editable || false,
    /* true允许编辑文字 */
    iconData: opts.iconData || [],
    listData: opts.listData || [],
    listNo: opts.listNo || [],
    bindType: opts.bindType || false,
    /* true默认显示0.0 */
  };

  var show_hide_type = 0; /* 0之前加载，1之后完成 */
  var ArrayY = [],
    ArrayX = [],
    ArrayYX = {},
    Xmax = 0,
    Xmin = 0,
    Ymax = 0,
    Ymin = 0;
  var listJson = [];
  var listIid = [];
  var classIcon = [];
  var listNo = option.listNo;
  var findDataNo = []; //实时数据no编号列表
  var intervalBlink = '';
  var blinkData = [];
  var blinkTime = 500; //多少毫秒每次动态闪烁 ，每帧闪烁多少毫秒
  var blinkSetMinute = 3; //动态闪烁3分钟后停止
  var blinkStartData = {}; // 刷新初页面始化变色的图标，不再次变色
  var preDataValue = {};

  if (option.iconData) {
    listJson = option.iconData.listJson;
    listIid = option.iconData.listIid;
    classIcon = option.iconData.class_icon;
  }

  opts = opts === undefined ? {} : opts;
  this.init = function() {};

  // 实时数据,状态量值
  this.real_data = function(call) {
    blinkData = [];
    var getData;
    // var noInfo = [
    //   { no: 47, name: 0.0 },
    //   // 变红色
    //   { no: 48, name: 1.0 },
    //   { no: 49, name: 0.0 },
    //   { no: 50, name: 0.0 },
    // ];
    // myAjax({
    //     requestType: 'post',
    //     url: requestUrl.u_bind,
    //     data: get_ajax_data,
    //     async: false,
    //     success: function(data) {
    //         getData = JSON.parse(data);
    //     }
    // });
    $.ajax({
      type: 'POST',
      // dataType: 'html',
      // dataType : "json",
      url: requestUrl.u_bind,
      data: get_ajax_data,
      async: false,
      xhrFields: {
        withCredentials: true,
      },
      beforeSend: function(request) {
        request.setRequestHeader('Authorization', getCookie('token'));
      },
      // data: {id: 1924015},
      success: function(data) {
        // let infoDate = [
        //   { no: 10, name: 1.0 },
        //   { no: 9, name: 0.0 },
        //   { no: 8, name: 0.0 },
        //   { no: 7, name: 1.0 },
        //   { no: 6, name: 1.0 },
        //   { no: 5, name: 1.0 },
        //   { no: 2, name: 1.0 },
        //   { no: 4, name: 1.0 },
        //   { no: 3, name: 0.0 },
        //   { no: 1, name: 1.0 },
        // ];

        // getData = noInfo;
        // getData = data.data.push(noInfo);
        getData = data.data;
        console.log(getData);
      },
    });
    for (var i = 0; i < getData.length; i++) {
      // 返回的数据
      var d = getData[i];
      findDataNo[d.no] = i;
      // console.log(d);
      // debugger;
      if (d['name'] === undefined) {
        continue;
      }
      var getData_ok = this.findNo(d['no']);
      if (!getData_ok || !getData_ok.data) {
        continue;
      }
      // console.log('这是对应的值对吗？');
      // console.log(getData_ok);
      switch (getData_ok.data.option) {
        case 'circle':
          if (getData_ok.data.status) {
            if (getData_ok.data.status[d['name']]) {
              getData_ok.data
                .set('stroke', getData_ok.data.status[d['name']])
                .setCoords();
              canvas.requestRenderAll();
            }
          }
          break;
        case 'line':
          console.log('line', getData_ok);
          var self_g = this;
          this.blinkFunColor({
            d: d,
            getData_ok: getData_ok,
            calls: function(e) {
              if (getData_ok.data.status) {
                if (getData_ok.data.status[d['name']]) {
                  getData_ok.data
                    .set('stroke', getData_ok.data.status[d['name']])
                    .setCoords();
                  canvas.requestRenderAll();
                }
              }
            },
            calls_status: function(getDataS) {
              getData_ok.data
                .set(
                  'stroke',
                  getDataS.data.status[getDataS.data.colourBlinkType],
                )
                .setCoords();
            },
          });

          break;
        case 'group_ms':
        case 'icon_group':
          var self_this = this;
          var self_rec = this;
          this.blinkFunColor({
            d: d,
            getData_ok: getData_ok,
            calls: function(e) {
              self_this.get_json_icon_group(getData_ok, d['name']);
            },
            calls_status: function(getDataS) {
              /* 中间连续 */
              self_rec.recursion_color(
                getDataS.data,
                getDataS.data.status[getDataS.data.colourBlinkType],
              );
            },
          });
          break;
        case 'text':
        case 'text_edit':
          // console.log('@@@@@@@@@@@@@@');
          // console.log(d['name']);
          // d['name']
          // ? d['name'].toString().match(/^\d+(?:\.\d{0,1})?/)
          // : d['name'],
          this.get_json_text(
            getData_ok,
            d['name']
              ? d['name'].substring(0,d['name'].indexOf(".")+2)
              : d['name'],
          );
          break;
        default:
          if (getData_ok.data.status) {
            if (getData_ok.data.status[d['name']]) {
              getData_ok.data
                .set('backgroundColor', getData_ok.data.status[d['name']])
                .setCoords();
              getData_ok.data
                .set('fill', getData_ok.data.status[d['name']])
                .setCoords();
              canvas.requestRenderAll();
            }
          }
          break;
      }
    }

    if (call) {
      show_hide_type = 60;
      call(show_hide_type);
    }
    for (var p = 0; p < getData.length; p++) {
      var get_pre = getData[p];
      preDataValue[get_pre['no']] = get_pre;
    }
  };
  function getCookie(key) {
    var strCookie = document.cookie;
    //将多cookie切割为多个名/值对
    var arrCookie = strCookie.split('; ');
    for (var i = 0; i < arrCookie.length; i++) {
      var tmp = arrCookie[i].split('=');
      if (tmp[0] == key) {
        return tmp[1];
      }
    }
  }
  /* 开关量图标闪烁 ,参数：calls开始或者结束，calls_status中间连续
   * 1合闸，0分闸，2未知
   * */
  this.blinkFunColor = function(opts_blink) {
    var opts_b = {
      calls:
        opts_blink.calls ||
        function() {
          return false;
        },
      calls_status:
        opts_blink.calls_status ||
        function() {
          return false;
        },
      d: opts_blink.d || '',
      getData_ok: opts_blink.getData_ok || '',
    };
    var d = opts_b.d;
    var getData_ok = opts_b.getData_ok;

    var id_val = d['no'];
    var get_current_value = d['name'];
    var get_pre_value = this.findNoPreDataValue(id_val);

    if (blinkStartData[id_val] == undefined) {
      // console.log("初始化第一次变化颜色，不再次变化颜色",blinkStartData[id_val],12345678);
      this.get_json_icon_group(getData_ok, get_current_value);
      blinkStartData[id_val] = 1;
    }
    if (get_pre_value != undefined) {
      // console.log(333);

      // str = "048";
      str = '02468'; // 1奇数表示红色合闸异常，0偶数表示绿色分闸正常
      var aa = str.indexOf(get_pre_value.name) != -1 ? 0 : 1;
      var bb = str.indexOf(get_current_value) != -1 ? 0 : 1;

      if (aa !== bb || id_val == get_find_no) {
        get_find_no = -1;
        var timeColorBlink = 0;

        /* 500毫秒乘以2等于1秒，2乘以180秒等于3分钟 */
        // for (var t = 1; t <= 2 * 180; t++) {
        for (var t = 1; t <= 2 * 60 * blinkSetMinute; t++) {
          /* 开关量事件变化颜色 */
          setTimeout(
            function() {
              if (blinkClearTimeoutType == 1) {
                // 鼠标抬起，动态闪烁
                /* 中间连续 动态闪烁 opts_b.calls_status */
                this.blinkStatusWait(getData_ok, opts_b.calls_status);
              } else {
                // 鼠标按下，忽略动态闪烁
              }
            }.bind(this),
            blinkTime * t,
          );

          timeColorBlink = blinkTime * (t + 1);
        }
        /* 结束 */
        setTimeout(
          function() {
            opts_b.calls('end');
          }.bind(this),
          timeColorBlink,
        );
      }
    } else {
      /* 开始刷新或者初始化 */
      setTimeout(
        function() {
          // console.log("初始化",getData_ok,d['name']);
          opts_b.calls('start');
        }.bind(this),
        500,
      );
    }
  };
  // 闪烁状态功能
  this.blinkStatus = function(blinkData) {
    clearInterval(intervalBlink);
    if (blinkData.length == 0) {
      //判断闪烁状态是否存在，如果不存在则终止程序
      return false;
    }
    // 闪烁  1正常：绿色，0异常：红绿闪烁
    intervalBlink = setInterval(
      function() {
        for (var i = 0; i < blinkData.length; i++) {
          var getDataS = blinkData[i];
          if (!getDataS || !getDataS.data) {
            continue;
          }

          if (getDataS.data.status === undefined) {
            return true;
          } /* home和index页面不一样，index出错问题 */
          this.recursion_color(
            getDataS.data,
            getDataS.data.status[getDataS.data.colourBlinkType],
          );
          if (getDataS.data.colourBlinkType) {
            getDataS.data.colourBlinkType = 0;
          } else {
            getDataS.data.colourBlinkType = 1;
          }
        }
        canvas.requestRenderAll();
      }.bind(this),
      blinkTime,
    );
  };

  /* 开关量闪烁限制三分钟 */
  this.blinkStatusWait = function(blinkData, call) {
    var getDataS = blinkData;
    if (!getDataS || !getDataS.data) {
      return false;
    }
    if (getDataS.data.status === undefined) {
      return true;
    } /* home和index页面不一样，index出错问题 */
    if (getDataS.data.colourBlinkType) {
      getDataS.data.colourBlinkType = 0;
    } else {
      getDataS.data.colourBlinkType = 1;
    }
    call(getDataS);

    canvas.requestRenderAll();
  };

  this.getsss = function(no) {};

  // 1图标回路状态，开关量，状态颜色
  this.get_json_icon_group = function(getData, name) {
    if (getData.data.status === undefined) {
      return true;
    } /* home和index页面不一样，index出错问题 */
    if (getData.data.colourBlink && name == 0) {
      // 闪烁跳循环
      return true;
    } else {
      if (getData.data.status[name] === undefined) {
        this.recursion_color(getData.data, '#FFFF00');
      } else {
        this.recursion_color(getData.data, getData.data.status[name]);
      }
    }
    canvas.requestRenderAll();
  };
  /* 2递归函数加载颜色配置 ,开关量，状态颜色*/
  this.recursion_color = function(data, color) {
    if (data._objects) {
      for (var i = 0; i < data._objects.length; i++) {
        this.recursion_color(data._objects[i], color);
      }
    } else {
      data.set('fill', color).setCoords();
      data.set('stroke', color).setCoords();
    }
  };
  /* 自动化：递归函数获取_objects */
  this.get_Object_find = function(data) {
    for (var list in data) {
      if (list.option == 'icon_group' || list.option == 'rect') {
        if (list.option == 'rect') {
          return list;
        }
        if (list.iid) {
          data = this.get_Object_find(list._objects);
        } else {
          return data;
        }
      }
    }
    return data;
  };

  this.get_json_text = function(getData, name) {
    if (name) {
      getData.data.set('text', name.toString()).setCoords();
      canvas.requestRenderAll();
    }
  };

  /* 测试更新颜色的状态 */
  this.get_json = function(no) {
    var getId = getDataId[no];
    var data = allObject[getId];
    var groupList = data.data._objects;
    if (!groupList) {
      return;
    }
    for (var list in groupList) {
      //                console.log(data);
      if (list.type == 'rect') {
        list.set('fill', 'GreenYellow').setCoords();
        canvas.requestRenderAll();
        break;
      }
    }
  };

  this.init_show = function(call) {
    console.log('这是获取到的值');
    console.log(getDataJson);
    var data = getDataJson;
    if (data[0] == null) {
      return false;
    }
    return this.init_data_show(data, call);
  };
  this.init_data_show = function(data, call) {
    if (!data) {
      return;
    }
    var self = this;
    // var getTest=[];
    setTimeout(function() {
      for (var i = 0; i < data.length; i++) {
        let list = data[i]; //注意，var变量不支持拓扑图，let变量支持拓扑图；因为var变量是异步的，let变量是同步的，fabric.Image.fromURL里面全局变量同步
        if (!option.selectable) {
          list['selectable'] = false;
        }
        getDataId[list.no] = i;
        list['key_usable'] = 1;
        var map = self.optionDraw(list);
        if (!map) {
          continue;
        }
        if (list.option == 'icon_group') {
          if (list.did) {
            map.draw.set('did', list.did);
          }
        }

        // if(map.draw.option=='line'){
        //     // console.log(map.draw);
        //     map.draw.set('strokeDashArray',[6,7]);
        //     getTest.push(i);
        // }

        if (list.option == 'img') {
          var oImg = [];
          var setImg = {};
          var getImg = list.url;
          console.log(fabric.Image);
          fabric.Image.fromURL(
            host_url + 'picture/' + getImg + '.png',
            function(img) {
              oImg = img.set(list);
              self.setAddObject(oImg, 'img');
            },
          );
        } else {
          self.setAddObject(map.draw, map.type);
        }
      }

      // var ii=0;
      // setInterval(function () {
      //     console.log(ii);
      //     for(var ll=0;ll<getTest.length;ll++){
      //         var getData_ok=allObject[ll];
      //         console.log(getData_ok);
      //         if (ii==0){
      //             getData_ok.data['key_usable'] = 1;
      //             getData_ok.data.set('fill','white').setColor();
      //             getData_ok.data.set('stroke','white').setColor();
      //             getData_ok.data.set('strokeDashArray',[6,7]);
      //             ii=1;
      //         }else {
      //             getData_ok.data.set('fill','red').setColor();
      //             getData_ok.data.set('stroke','red').setColor();
      //             getData_ok.data.set('strokeDashArray',[6,7]);
      //             ii=0;
      //         }
      //         // console.log(ll);
      //         canvas.requestRenderAll();
      //     }
      // },500);

      ArrayYX = {
        ymax: ArrayY.max(),
        ymin: ArrayY.min(),
        xmax: ArrayX.max(),
        xmin: ArrayX.min(),
        height: ArrayY.max() - ArrayY.min(),
        width: ArrayX.max() - ArrayX.min(),
      };
      show_hide_type = 30;

      if (option.moduleType) {
        self.resizeFabricSize(function(status) {});
        getArrayYX = ArrayYX;
        getArray_width = getArrayYX.width + 500;
        getArray_height = getArrayYX.height + 500;
        canvas.setWidth(getArray_width);
        canvas.setHeight(getArray_height);
        $('#loading').fadeToggle(1);
      }

      return ArrayYX;
    }, 0);
  };

  this.resizeFabricSize = function(call) {
    var h = ArrayYX.height;
    var w = ArrayYX.width;

    if (w < 300) {
      return false;
    }

    if (!mobile_bs.versions.mobile) {
      /* 自适应页面 */
      var delta = new fabric.Point(
        this.judgeSign(ArrayYX.xmin) * 0.9,
        this.judgeSign(ArrayYX.ymin),
      );
      canvas.relativePan(delta);
      var zoom = canvas.getZoom();
      var zoomPoint = new fabric.Point(0, 0);
      canvas.zoomToPoint(zoomPoint, zoom);
      canvas.setWidth(window.innerWidth + zoom * 4600);
      canvas.setHeight(window.innerHeight + zoom * 4600);

      if (call) {
        show_hide_type = 100;
        call(show_hide_type);
      }
    } else {
      var delta = new fabric.Point(
        this.judgeSign(ArrayYX.xmin) * 0.9,
        this.judgeSign(ArrayYX.ymin),
      );
      canvas.relativePan(delta);
      canvas.zoomToPoint({ x: 0.1, y: 0 }, (window.innerWidth / w) * 3);
    }
  };

  Array.prototype.max = function() {
    return Math.max.apply({}, this);
  };
  Array.prototype.min = function() {
    return Math.min.apply({}, this);
  };

  /* js如何判断一个数字是正数还是负数    -0.1 ; 5 ; true
   * 反数字
   * */
  this.judgeSign = function(num) {
    var reg = new RegExp('^-?[0-9]*.?[0-9]*$');
    if (reg.test(num)) {
      var absVal = Math.abs(num);
      return num == absVal ? -num : Math.abs(num);
    } else {
      return false;
    }
  };

  /* 左边导航图标 */
  this.button_canvas_icon_arrows = function(get_type) {
    var type = 0;
    if (get_type !== undefined) {
      type = 1;
    }

    var html = '';
    var add_id = '';

    var list = {
      colour_status: 'red',
      fill: 'rgb(0,0,0)',
      group_type: true,
      iid: 15,
      key_id: 1,
      key_usable: 1,
      no: 2,
      // width:1,height:1,
      scaleX: 1,
      option: 'icon_group',
      top: 1,
      left: 1,
    };

    $('#add_all_icon_id').html('');

    var html_icon = '';
    for (var ci = 0; ci < classIcon.length; ci++) {
      var class_data = classIcon[ci];
      html_icon +=
        '<div class="icontype"><div class="icontype-head triangle-close">' +
        class_data.class_name +
        '</div><div class="icontype-content" style="display: none;"><div class="canvas-list" id="class_data_id_' +
        class_data['cid'] +
        '"></div></div>';
    }
    $('.c-list').append(html_icon);

    for (var ic = 0; ic < listJson.length; ic++) {
      var getAllIcon = listJson[ic];
      var getName = getAllIcon.name;
      var getIconClass = getAllIcon.icon_class;
      if (getAllIcon.icon_type != 1) {
        add_id = 'class_icon_' + ic;
        html =
          '<div class="canvas-list-container addIconGroup" iid=' +
          getAllIcon.iid +
          ' icon_type=' +
          getAllIcon.icon_type +
          '><div class="canvas-container"><div class="canvas-icon"><i><img class="picture_class" src="' +
          host_url +
          'picture/' +
          getAllIcon.iid +
          '.png"></i></div><span>' +
          getName +
          '</span></div>';
        $('#class_data_id_' + getIconClass).append(html);
      } else {
        add_id = 'class_icon_' + ic;
        html =
          '<div  class="canvas-list-container addIconGroup" iid=' +
          getAllIcon.iid +
          ' icon_type=' +
          getAllIcon.icon_type +
          '><canvas id="' +
          add_id +
          '" width="70" height="50"></canvas><span>' +
          getName +
          '</span></div>';
        $('#class_data_id_' + getIconClass).append(html);

        var map = this.createIconGroup(getAllIcon, list, type);
        map.draw.set('name', getName);
        new fabric.Canvas(add_id).add(map.draw);
      }
    }
  };
  this.optionDraw = function(list) {
    var map = false;
    switch (list.option) {
      case 'circle':
        map = { draw: new fabric.Circle(list), type: 'circle' };
        break;
      case 'img':
        map = { draw: list, type: 'img' };
        break;
      case 'rect':
        map = { draw: new fabric.Rect(list), type: 'rect' };
        map.draw.set('strokeWidth', 0); /* 注意增加1问题，改为1 */
        break;
      case 'arrows':
        map = this.arrows(list);
        break;
      case 'line':
        map = this.create_line(list);
        break;
      case 'text':
        map = this.create_text(list);
        break;
      case 'text_edit':
        map = this.create_text_edit(list);
        var str = map.draw.name; //用户输入
        if (/^[\u4e00-\u9fa5]+$/i.test(str)) {
          map.draw.set('fontFamily', '宋体');
        }
        break;
      case 'table':
        map = this.create_line(list);
        break;
      case 'triangle':
        map = this.triangle(list);
        break;
      case 'arrows_button':
        map = this.arrows_button(list);
        break;
      case 'icon_group':
        map = this.oneIconFind(list);
        break;
      case 'group_ms':
        list['group_type'] = true;
        if (!list.gd) {
          return false;
        }
        var group_ms_data = this.group_ms_data(list.gd);

        map = { draw: new fabric.Group(group_ms_data, list), type: 'group_ms' };
        break;
      default:
        break;
    }
    return map;
  };

  /* 处理合并数据 */
  this.group_ms_data = function(data) {
    var get_group_data = [];
    for (var i = 0; i < data.length; i++) {
      var v = data[i];
      get_group_data.push(
        this.optionDraw(v)['draw'],
      ); /* 注意：不支持$.each(data,function (k,v)，否则无效 */
    }
    return get_group_data;
  };
  /* 处理分拆数据 */
  this.group_ms_split = function(data) {};

  this.triangle = function(list) {
    var line1 = new fabric.Line([135, 278, 144, 266], {
      fill: 'red',
      stroke: list.stroke,
      strokeWidth: 1,
      //                selectable: true,/* true允许拖拽    false停止拖拽 */
      globalCompositeOperation: 'destination-over',
      option: 'triangle',
      hasControls: false,
    });

    var line2 = new fabric.Line([135, 278, 154, 278], {
      fill: 'red',
      stroke: list.stroke,
      strokeWidth: 1,
      //                selectable: true,/* true允许拖拽    false停止拖拽 */
      globalCompositeOperation: 'destination-over',
      option: 'triangle',
      hasControls: false,
    });

    var line3 = new fabric.Line([153, 278, 143, 266], {
      fill: 'red',
      stroke: list.stroke,
      strokeWidth: 1,
      //                selectable: true,/* true允许拖拽    false停止拖拽 */
      globalCompositeOperation: 'destination-over',
      option: 'triangle',
      hasControls: false,
    });

    var group = new fabric.Group([line1, line2, line3], list);
    return { draw: group, type: 'triangle' };
  };

  this.arrows_button = function(list) {
    var line1 = new fabric.Polyline(list.line_point, list);
    return { draw: line1, type: 'arrows_button' };
  };
  /* start绘画 */
  this.arrows = function(list) {
    var rect = {
      left: 30,
      top: 30,
      width: 30,
      height: 9,
      fill: list.colour_status,
    };
    var rect1 = new fabric.Rect(rect);

    var line = new fabric.Line([20, 35, 30, 35], {
      fill: list.fill,
      stroke: list.stroke,
      strokeWidth: 1,
      selectable: false,
      globalCompositeOperation: 'destination-over',
    });
    var line1 = new fabric.Polyline(
      [
        { x: 60, y: 35 },
        { x: 70, y: 35 },
        { x: 65, y: 30 },
        { x: 80, y: 35 },
        { x: 65, y: 40 },
        { x: 70, y: 35 },
      ],
      {
        fill: list.fill,
        stroke: list.stroke,
        strokeWidth: 1,
        selectable: false,
        globalCompositeOperation: 'destination-over',
      },
    );
    var group = new fabric.Group([rect1, line, line1], list);
    return { draw: group, type: 'arrows' };
  };
  this.create_line = function(list) {
    var line = new fabric.Line(list.line_point, list);
    return { draw: line, type: 'line' };
  };
  this.create_text = function(list) {
    if (option.bindType) {
      if (list['binding']) {
        list['name'] = '0.0';
      }
    }
    if (list.name) {
      var text = new fabric.Text(list.name, list);
      return { draw: text, type: 'text' };
    }
    return false;
  };
  this.create_text_edit = function(list) {
    if (option.bindType) {
      if (list['binding']) {
        list['name'] = '------';
      }
    }
    list['strokeWidth'] = 0;
    if (list.name) {
      list['editable'] = option.editable;
      var text = new fabric.Textbox(list.name, list);
      return { draw: text, type: 'text_edit' };
    }
    return false;
  };
  this.setAddObject = function(data, type) {
    canvas.add(data);
    if (
      !option.hasControls ||
      type == 'text' ||
      type == 'arrows' ||
      type == 'triangle'
    ) {
      canvas.item(canvas._objects.length - 1).hasControls = false;
    }

    if (!option.selectable) {
      canvas.item(canvas._objects.length - 1).selectable = false;
    }
    allObject.push({ data: data, type: type });
    allObject[allObject.length - 1].data.set('key_id', allObject.length - 1);

    ArrayY.push(data.top);
    ArrayX.push(data.left);
    return data;
  };

  /* 根据no查询 */
  this.findNo = function(no) {
    if (listNo.length === 0) {
      return false;
    }
    return allObject[listNo[no]];
  };
  /* 根据no查询上一次的数据返回值 */
  this.findNoPreDataValue = function(no) {
    // console.log(listNo);
    if (preDataValue.length === 0) {
      return false;
    }
    return preDataValue[no];
  };
  /* 根据iid查询 */
  this.queryJsonDataId = function(iid) {
    if (listIid.length === 0) {
      return false;
    }
    return listJson[listIid[iid]];
  };

  /* 单查询icon图标 */
  this.oneIconFind = function(list) {
    var iid = list.iid;
    var data = this.queryJsonDataId(iid);
    var map = this.createIconGroup(data, list);
    return map;
  };

  this.createIconGroup = function(data, list, get_type) {
    var type = 0;
    if (get_type) {
      type = 1; /* 禁止被选中 */
    }
    if (!data) {
      return;
    }
    var left_value = list.left;
    var top_value = list.top;
    var angle_value = list.angle ? list.angle : 0;
    var group = [];
    var group_map = [];
    var listData = data;
    var iid = listData.iid;
    var getDataJson = JSON.parse(listData.json);
    if (listData.json == '') {
      return false;
    }
    for (var i = 0; i < getDataJson.length; i++) {
      var setlistJson = getDataJson[i];
      if (!option.selectable) {
        setlistJson['selectable'] = false;
      }
      getDataId[setlistJson.no] = i;

      setlistJson['key_id'] = i;
      setlistJson['key_usable'] = 1;
      var map = this.optionDraw(setlistJson);
      if (!map) {
        continue;
      }
      group_map.push(map.draw);
    }
    var set_group = {
      iid: iid,
      no: list.no,
      angle: angle_value,
      name: list.name,
      binding: list.binding,
      key_id: 0,
      key_usable: 1,
      left: left_value,
      top: top_value,
      option: 'icon_group',
      // selectable:false,
      // width:60,
      // height:40,
      // zoom:0.1,
      // scaleX:0.5,
      // scaleY:0.5,
      group_type: true,
      hasControls: false,
    };
    if (type === 1) {
      set_group['selectable'] = false;
    }

    if (list.status) {
      set_group['status'] = list.status;
    }

    if (list.colourBlink) {
      set_group['colourBlink'] = list.colourBlink;
      set_group['colourBlinkType'] = 1;
    } else {
      set_group['colourBlink'] = false;
      set_group['colourBlinkType'] = 0;
    }

    if (list.opacity) {
      set_group['opacity'] = list.opacity;
    }
    group = new fabric.Group(group_map, set_group);
    var map = { draw: group, type: 'icon_group' };
    return map;
  };
}
